using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using UberChess.Structure.Pieces;

namespace UberChess
{
   /// <summary>
   /// This is the main type for your game
   /// </summary>
   public class Game1 : Microsoft.Xna.Framework.Game
   {
       GraphicsDeviceManager graphics;
       SpriteBatch spriteBatch;
       private Texture2D titleScreenWhite;
       private Texture2D titleScreenBlack;
       private Texture2D titleScreenMultiplayer;
       private Texture2D backGround;
       private Texture2D spriteSheet;
       private Texture2D spriteUndo;
       private SpriteFont font;

       private GameBoard gameBoard { get; set; }
       public enum GameState { TitleScreenWhite, TitleScreenBlack, TitleScreenMultiplayer, Playing };
       public GameState gameState { get; set; }
       double elapsedTimeSinceLastInput = 0;
       int minimumTimeBetweenInput = 250;
       public bool isFullScreen { get; set; }


      public Game1()
      {
         graphics = new GraphicsDeviceManager(this);
         Content.RootDirectory = "Content";
         gameState = GameState.TitleScreenWhite;
      }

      /// <summary>
      /// Allows the game to perform any initialization it needs to before starting to run.
      /// This is where it can query for any required services and load any non-graphic
      /// related content.  Calling base.Initialize will enumerate through any components
      /// and initialize them as well.
      /// </summary>
      protected override void Initialize()
      {
         // TODO: Add your initialization logic here
         base.Initialize();
         graphics.PreferredBackBufferWidth = 1137;
         graphics.PreferredBackBufferHeight = 741;
         graphics.ApplyChanges();

          this.IsMouseVisible = true;
      }

      /// <summary>
      /// LoadContent will be called once per game and is the place to load
      /// all of your content.
      /// </summary>
      protected override void LoadContent()
      {
         // Create a new SpriteBatch, which can be used to draw textures.
         spriteBatch = new SpriteBatch(GraphicsDevice);
         titleScreenWhite = Content.Load<Texture2D>(@"Textures\TitleScreenWhite");
         titleScreenBlack = Content.Load<Texture2D>(@"Textures\TitleScreenblack");
         titleScreenMultiplayer = Content.Load<Texture2D>(@"Textures\TitleScreenMulti");
         backGround = Content.Load<Texture2D>(@"Textures\Background");
         spriteSheet = Content.Load<Texture2D>(@"Textures\Spritesheet");
          spriteUndo = Content.Load<Texture2D>(@"Textures\UndoSprite");
          font = Content.Load<SpriteFont>(@"Fonts\Segoe");
         NewGame();

         // TODO: use this.Content to load your game content here
      }

      private void NewGame()
      {
         gameBoard = new GameBoard(spriteSheet, this, font, spriteUndo);
      }

      /// <summary>
      /// UnloadContent will be called once per game and is the place to unload
      /// all content.
      /// </summary>
      protected override void UnloadContent()
      {
         // TODO: Unload any non ContentManager content here
      }

      /// <summary>
      /// Allows the game to run logic such as updating the world,
      /// checking for collisions, gathering input, and playing audio.
      /// </summary>
      /// <param name="gameTime">Provides a snapshot of timing values.</param>
      protected override void Update(GameTime gameTime)
      {
          KeyboardState keyboardState = Keyboard.GetState();
          switch(gameState){
              case GameState.TitleScreenWhite:
                ListenForFullScreen();
                if (keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.Up))
                {
                    elapsedTimeSinceLastInput -= gameTime.ElapsedGameTime.TotalMilliseconds;
                    if (elapsedTimeSinceLastInput <= 0)
                    {
                        if (keyboardState.IsKeyDown(Keys.Space))
                        {
                            NewGame();
                            gameState = GameState.Playing;
                            gameBoard.SetPlayerSide(Side.WHITE);
                        }
                        else if (keyboardState.IsKeyDown(Keys.Down))
                        {
                            NewGame();
                            gameState = GameState.TitleScreenBlack;
                            elapsedTimeSinceLastInput = minimumTimeBetweenInput;
                           
                        }
                        else if (keyboardState.IsKeyDown(Keys.Up))
                        {
                            NewGame();
                            gameState = GameState.TitleScreenMultiplayer;
                            elapsedTimeSinceLastInput = minimumTimeBetweenInput;
                           
                        }
                    }
                }
                else
                {
                    elapsedTimeSinceLastInput = 0;
                }
                  break;
              
              case GameState.TitleScreenBlack:
                  ListenForFullScreen();
                  if (keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.Up))
                  {
                      elapsedTimeSinceLastInput -= gameTime.ElapsedGameTime.TotalMilliseconds;
                      if (elapsedTimeSinceLastInput <= 0)
                      {
                          if (keyboardState.IsKeyDown(Keys.Space))
                          {
                              gameState = GameState.Playing;
                              gameBoard.SetPlayerSide(Side.BLACK);
                          }
                          else if (keyboardState.IsKeyDown(Keys.Down))
                          {
                              gameState = GameState.TitleScreenMultiplayer;
                              elapsedTimeSinceLastInput = minimumTimeBetweenInput;
                          }
                          else if (keyboardState.IsKeyDown(Keys.Up))
                          {
                              gameState = GameState.TitleScreenWhite;
                              elapsedTimeSinceLastInput = minimumTimeBetweenInput;
                          }
                      }
                  }
                  else
                  {
                      elapsedTimeSinceLastInput = 0;
                  }
                  break;

              case GameState.TitleScreenMultiplayer:
                  ListenForFullScreen();
                  if (keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.Up)){
                  elapsedTimeSinceLastInput -= gameTime.ElapsedGameTime.TotalMilliseconds;
                      if (elapsedTimeSinceLastInput <= 0)
                      {
                          if (keyboardState.IsKeyDown(Keys.Space))
                          {
                              gameState = GameState.Playing;
                              gameBoard.SetMultiplayer(true);
                              gameBoard.SetPlayerSide(Side.WHITE);
                          }
                          else if (keyboardState.IsKeyDown(Keys.Down))
                          {
                              gameState = GameState.TitleScreenWhite;
                              elapsedTimeSinceLastInput = minimumTimeBetweenInput;
                          }
                          else if (keyboardState.IsKeyDown(Keys.Up))
                          {
                              gameState = GameState.TitleScreenBlack;
                              elapsedTimeSinceLastInput = minimumTimeBetweenInput;
                          }

                      }
                  }
                  else
                  {
                      elapsedTimeSinceLastInput = 0;
                  }
                  break;
                                
              case GameState.Playing:
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

                ListenForFullScreen();
                gameBoard.Update(gameTime);
                base.Update(gameTime);
                break;
          }
      }

       private void ListenForFullScreen()
      {
         if (Keyboard.GetState().IsKeyDown(Keys.F12))
         {
            graphics.ToggleFullScreen();
            isFullScreen = graphics.IsFullScreen;
         }
      }

      /// <summary>
      /// This is called when the game should draw itself.
      /// </summary>
      /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            switch (gameState)
            {
                case GameState.TitleScreenWhite:
                    spriteBatch.Draw(titleScreenWhite, new Rectangle(0, 0, 1137, 741), Color.White);
                    break;
                case GameState.TitleScreenBlack:
                    spriteBatch.Draw(titleScreenBlack, new Rectangle(0, 0, 1137, 741), Color.White);
                    break;
                case GameState.TitleScreenMultiplayer:
                    spriteBatch.Draw(titleScreenMultiplayer, new Rectangle(0, 0, 1137, 741), Color.White );
                    break;
                case GameState.Playing:                    
                    spriteBatch.Draw(backGround, new Rectangle(0, 0, 1137, 741), Color.White);
                    gameBoard.Draw(spriteBatch);                    
                    break;
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
